﻿using System.Collections.Generic;
using UnityEngine;

namespace ABA
{
    /// <summary>
    /// 全局单例对象（非线程安全）
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public abstract class Singleton<T> : ISingleton where T : Singleton<T>, new()
    {
        protected static T _inst = default(T);

        public static T inst
        {
            get
            {
                if (null == _inst)
                {
                    _inst = new T();
                    _inst.Init();
#if UNITY_EDITOR
                    Debug.Log($"Singleton inst:{typeof(T).Name}");
#endif
                    SingletonManager.Retain(_inst);
                }
                return _inst;
            }
        }

        public static bool IsValid
        {
            get
            {
                return _inst != null;
            }
        }

        protected Singleton()
        {

        }

        protected virtual void Init()
        {

        }

        public virtual void Active()
        {

        }

        public virtual void Release()
        {
            if (_inst != null)
            {
                SingletonManager.Release(_inst);
                _inst = null;
            }
        }
    }
}